/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#include <QObject>
#include <vector>

#include "version.h"
#include "ftpdownloader.h"
#include "httpdownloader.h"
#include "installer.h"

class Controller : public QObject {
	Q_OBJECT

	private:
		enum CurrentDownloadType { HTTP, FTP};

		CurrentDownloadType download_type;

		FTPDownloader ftp_downloader; /// FTP file downloader
		HTTPDownloader http_downloader; /// HTTP file downloader
		Installer installer; /// Application installer
		std::vector<Version> versions; /// Versions

		bool m_abort; /// Stop working when done with current job
		bool m_download_only; /// Only download applications

		Version currentVersion; /// Version currently being applied

	public:
		Controller(QObject *parent = 0);
		~Controller();

		/**
			Download single version.

			@param version version to download
		*/
		void download(Version &version);

		/**
			Internal controller connections.
		*/
		void connectController();

	public slots:
		/**
			Start downloading versions.

			@param version version to process
			@param download_only only download files
		*/
		void apply(std::vector<Version> versions, bool download_only);

		/**
			Prevent the next download from starting.

			Allows current download to finish.
		*/
		void abort();

	private slots:
		/**
			Apply next requested application.
		*/
		void applyNext();

		/**
			Install current application version.

			@param error was there an error downloading the application.
		*/
		void install(bool error=false);

	signals:
		/**
			Downloads finished properly.

			@param error there was an error
		*/
		void finished(bool error);

		/**
			Downloads aborted by user.
		*/
		void aborted();

		void dataReadProgress(int, int);

		/**
			Reports current status

			@param status status message
		*/
		void status(QString message);

		void currentProcess(QString name);
};
